package gui.dialogs;


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Alert extends Dialog {
	
	private Shell shell;
	private String title;
	private String question;

	/**
	 * Constuctor of an alert popup
	 * 
	 * @param parent Shell of the program
	 * @param title of the popup
	 * @param question content of the popup
	 */
	public Alert (Shell parent, String title, String question) {
		super (parent, 0);
		this.title = title;
		this.question = question;
	}
	
	/**
	 * This function opens a alert popup. The this alert popup shows a message and has only an ok button.
	 */
	public void open () {
		Shell parent = getParent();
		shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setText(title);
		
		shell.setLayout(new GridLayout(1, false));
		
		Text text = new Text(shell, SWT.READ_ONLY);
		text.setText(question);
		GridData textGridData = new GridData();
		textGridData.horizontalAlignment = GridData.CENTER;
		
		Composite buttonComposite = new Composite(shell, SWT.None);
		GridData bcdata = new GridData();
		bcdata.horizontalAlignment = GridData.CENTER; 
		buttonComposite.setLayoutData(bcdata);
		buttonComposite.setLayout(new GridLayout(1, true));
		
		Button confirmButton = new Button(buttonComposite, SWT.None);
		confirmButton.setText("Ok");
		confirmButton.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				closeDialog();
			}
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {}
		});
		
		shell.pack();
		shell.open();
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
	}
	
	/**
	 * this function closes the popup.
	 */
	protected void closeDialog() {
		shell.close();
	}
}
